package com.nju.bean.image;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.nju.bean.client.preference.C;
import com.nju.bean.ftp.ContinueFTP;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class ImageDeliver {
	static public void sendToserver(String local,String remote) throws IOException{
		ContinueFTP ftp = new ContinueFTP();
		ftp.connect(C.HOST_IP_STRING, "bean","bean");
		ftp.upload(local, remote);
		ftp.disconnect();
	}
	
	static public Drawable getFromServerInUrl(String id) throws MalformedURLException, IOException{
		String urlString = C.FTP_IN_URL+id+C.REMOTE_FILE_LAST_NAME;
		Log.v("getFromServerInUrl",urlString);
		Drawable drawable = Drawable.createFromStream(  
                new URL(urlString).openStream(), "image.jpg");
		return drawable;
	}
	
	static public Bitmap getFromServer(String id) throws IOException{
		ContinueFTP ftp = new ContinueFTP();
		Bitmap b;
		try {
			ftp.connect(C.HOST_IP_STRING, "bean","bean");
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
		try {
			File f= new File(C.PHOTO_PATH+File.separator+C.RECEIVE_FILE_NAME);
			if(f.exists())
				f.delete();
			ftp.download("photo/"+id+C.REMOTE_FILE_LAST_NAME, C.PHOTO_PATH+File.separator+C.RECEIVE_FILE_NAME);
			Log.v("ftp", "download");
			b = BitmapFactory.decodeFile(C.PHOTO_PATH+File.separator+C.RECEIVE_FILE_NAME);
		} catch (IOException e) {
			return null;
		}
		finally{
			ftp.disconnect();
		}
		return b;
	}
}
